package com.jj.fly.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.jj.fly.po.custom.AexUserCustom;

/**
 * 登录认证拦截器
 * 
 * @author JJ
 *
 */
public class LoginInterceptor implements HandlerInterceptor {

	// 不执行拦截的url
	public List<String> allowUrls;

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String requestUrl = request.getRequestURI();
		if (allowUrls != null && allowUrls.size() > 0) {
			for (String url : allowUrls) {
				if (requestUrl.contains(url)) {
					return true;
				}
			}
		}
		/*
		 * if (url.indexOf("login.action") >= 0 || url.indexOf("loginUI.action")
		 * >= 0) { // 如果进行登录提交，放行 return true; }
		 */
		// 判断session
		HttpSession session = request.getSession();
		// 从session中去取出用户信息
		AexUserCustom user = (AexUserCustom) session.getAttribute("user");
		if (user != null) {
			// 身份信息存在，放行
			return true;
		}
		// 执行到此处表示用户身份需要认证，跳转到登录页面
		request.getRequestDispatcher("/WEB-INF/jsp/login/login.jsp").forward(
				request, response);
		return false;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

	public void setAllowUrls(List<String> allowUrls) {
		this.allowUrls = allowUrls;
	}
}
